﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using libLite.Forms;

namespace libLite
{
    public partial class MainForm : Form
    {
        private string trazilica, zadnjaPretraga;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void srchBooks_Click(object sender, EventArgs e)
        {
            trazilica = "knaslov";
            srchBooks.Checked = true;
            srchUsers.Checked = false;
        }

        private void srchUsers_Click(object sender, EventArgs e)
        {
            trazilica = "uime";
            srchBooks.Checked = false;
            srchUsers.Checked = true;
        }

        private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            searchResults.Controls.Add(new dynKnjiga("aaa", "eee", "eee", "ee", Image.FromFile("c:\\korisnik.png")));
            searchResults.Controls.Add(new dynKnjiga("aaa", "eee", "eee", "ee", Image.FromFile("c:\\korisnik.png")));
            searchResults.Controls.Add(new dynKnjiga("aaa", "eee", "eee", "ee", Image.FromFile("c:\\korisnik.png")));
            searchResults.Controls.Add(new dynKnjiga("aaa", "eee", "eee", "ee", Image.FromFile("c:\\korisnik.png")));
        }

        private void addUser_Click(object sender, EventArgs e)
        {
            (new addUser()).Show();
        }

        private void editUser_Click(object sender, EventArgs e)
        {

        }

        private void addBook_Click(object sender, EventArgs e)
        {
            (new addBook()).Show();
        }

        private void editBooks_Click(object sender, EventArgs e)
        {

        }


        private void pretraga_Click(object sender, EventArgs e)
        {

        }

        private void pretraga_TextChanged(object sender, EventArgs e)
        {
            //if (pretraga.Text.Length < 2) ribbonClientPanel1.Visible = true; else ribbonClientPanel1.Visible = false;

            if (pretraga.Text.Length > 1)
            {
                searchResults.Controls.Clear();
                searchResults.Refresh();
                GC.Collect();

                string okle;
                string strUp;
                if (trazilica.Substring(0, 1) == "k")
                {
                    strUp = "select naslov,podnaslov,id,isbn from katalog where " + trazilica.Remove(0, 1) + " like '%" + pretraga.Text + "%' limit 0,15";
                    okle = "katalog";
                }
                else
                {
                    if (trazilica == "uime")
                        strUp = "select ime,prezime,id,username from korisnici where ime like '%" + pretraga.Text + "%' or prezime like '%" + pretraga.Text + "%' limit 0,15";
                    else
                        strUp = "select ime,prezime,id,username from korisnici where " + trazilica.Remove(0, 1) + " like '%" + pretraga.Text + "%' limit 0,15";
                    okle = "korisnici";
                }
                /*     upit = new OdbcCommand(strUp, KonWin.konekcija.ConObj);
                     try
                     {
                         citac = upit.ExecuteReader();
                         while (citac.Read())
                         {
                             // nalazi sliku od izabranog resursa
                             string strSql = "SELECT slika FROM " + okle + " where id=" + citac[2].ToString();
                             DataSet ds = new DataSet("Image");
                             OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql, KonWin.konekcija.ConObj);
                             OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP);
                             tempAP.Fill(ds, "Table");
                             byte[] buffer = (byte[])ds.Tables["Table"].Rows[0]["slika"];
                             MemoryStream stmBLOBData = new MemoryStream(buffer);
                             // Pozovemo da nam kreira button (sliku u listi)
                             ddDugme(citac[0].ToString(), citac[1].ToString(), citac[2].ToString(), citac[3].ToString(), Smanji((Bitmap)Image.FromStream(stmBLOBData), 108, 128));
                         }
                     }
                     catch (Exception ex) { MessageBox.Show("greska: " + ex); }*/
                zadnjaPretraga = pretraga.Text;
            }
        }

        private void searchResults_Paint(object sender, PaintEventArgs e)
        {

        }

        private void editBook_Click(object sender, EventArgs e)
        {

        }
    }
}
